﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 提交管理平台V1
{
    public partial class frmChangePassword : Form
    {
        SqlTools sqt;

        bool bCancel;

        string strUpdatePassword = "update UserList set Pwd='@密码' where Mname='TJGL' and Uname='@用户名称'";

        string strUserName;

        public frmChangePassword(string strTmpUserName)
        {
            InitializeComponent();
            if (strTmpUserName.Trim().Equals(string.Empty))
            {
                GeneralTools.ShowErrorMsg("用户名称不能为空！");
                this.Close();
            }
            strUserName = strTmpUserName;
            txtUserName.Text = strUserName;
        }

        private void frmChangePassword_Load(object sender, EventArgs e)
        {
            txtPassword.Focus();
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "DefualtDataBase"));
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            bCancel = true ;
            this.Close();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text.Equals(string.Empty))
            {
                GeneralTools.ShowInfoMsg("密码不能为空！");
                txtPassword.Focus();
                return;
            }
            if (!txtPassword.Text.Equals(txtRepassword.Text))
            {
                GeneralTools.ShowInfoMsg("两次输入的密码不一致，请重新输入！");
                txtPassword.Clear();
                txtRepassword.Clear();
                txtPassword.Focus();
                return;
            }
            if (sqt.DoCmdExecuteNonQuery(strUpdatePassword.Replace("@用户名称", txtUserName.Text)
                                                                                                    .Replace("@密码", txtPassword.Text)) > 0)
            {
                if (this.Text.Contains("修改"))
                {
                    GeneralTools.ShowInfoMsg("密码修改成功！");
                }
                else
                {
                    GeneralTools.ShowInfoMsg("密码设置成功！");
                }
                bCancel = false;
                this.Close();
            }
            else
            {
                if (this.Text.Contains("修改"))
                {
                    GeneralTools.ShowErrorMsg("密码修改失败：" + sqt.ErrMessage);
                }
                else
                {
                    GeneralTools.ShowErrorMsg("密码设置失败：" + sqt.ErrMessage);
                }
            }
        }

        public static DialogResult Show(string strTmpUserName,string strTmpTitle)
        {
            frmChangePassword newFrmChangePassword = new frmChangePassword(strTmpUserName);
            newFrmChangePassword.StartPosition = FormStartPosition.CenterParent;
            newFrmChangePassword.Text = strTmpTitle;
            newFrmChangePassword.ShowDialog();
            if (newFrmChangePassword.bCancel)
            {
                return DialogResult.Cancel;
            }
            else
            {
                return DialogResult.OK;
            }
        }
    }
}